package com.open.model;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * @author dashu
 * @date 2025/1/2 18:29
 * @description 分页参数
 */
@XmlType(name = "Page")
@XmlAccessorType(XmlAccessType.FIELD)
public class Page<E> implements Serializable {
    private static final long serialVersionUID = 3496103014062037454L;
    /**
     * 是否有下一页
     **/
    private boolean hasNext;
    /**
     * 是否有上一页
     **/
    private boolean hasPre;
    /**
     * 当前页包含的记录列表
     **/
    private List<E> items;
    /**
     * 当前页页码
     **/
    private int index = 1;
    private int size = 15;
    /**
     * 总页数
     **/
    private int totalPage;
    /**
     * 总条数
     **/
    private int total;
    /**
     * 对象
     **/
    private E result;

    public int getIndex() {
        return this.index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public boolean isHasPre() {
        return this.hasPre;
    }

    public void setHasPre(boolean hasPre) {
        this.hasPre = hasPre;
    }

    public boolean isHasNext() {
        return this.hasNext;
    }

    public void setHasNext(boolean hasNext) {
        this.hasNext = hasNext;
    }

    public List<E> getItems() {
        return this.items == null ? Collections.<E>emptyList() : this.items;
    }

    public void setItems(List<E> items) {
        this.items = items;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
        this.setTotalPage(total % size == 0 ? total / size : total / size + 1);
        this.setHasPre(this.index > 1);
        this.setHasNext(this.index < this.totalPage);
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public E getResult() {
        return result;
    }

    public void setResult(E result) {
        this.result = result;
    }

    public Page(int index, int size) {
        super();
        this.index = index;
        this.size = size;
    }

    public Page(int index) {
        super();
        this.index = index;
    }

    public Page() {
        super();
    }

    @Override
    public String toString() {
        return "Page [当前页数=" + index + ", 总页数=" + totalPage + ", 总条数=" + total + ", 大小=" + size + "]";
    }

}